<?PHP
/**
 * $Rev: 11 $
 * $Id: query.php 11 2013-03-05 13:03:52Z vad.oparin@gmail.com $
 */
class query extends behavior {

    public function getRoute($defaultRoute) {
        $defaultRoute = application::app()->getDefaultRoute();
        return array(
            'module' => ($_GET['_m']) ? $_GET['_m'] : $defaultRoute['module'],
            'controller' => ($_GET['_c']) ? $_GET['_c'] : $defaultRoute['controller'],
            'action' => ($_GET['_a']) ? $_GET['_a'] : $defaultRoute['action'],
            'parameters' => array_diff_key($_GET, array(
                '_m' => '',
                '_c' => '',
                '_a' => '',
            ))
        );
    }

    public function urlEncode($parameters) {
        foreach ($parameters as $key => $value) {
            $urlParams[] = urlencode($key) . '=' . urlencode($value);
        }
        return implode('&', $urlParams);
    }

    public function createUrl($route, $parameters = array()) {
        $defaultRoute = application::app()->getDefaultRoute();
        return SCRIPTNAME . '?' . $this->urlEncode(array_merge(array(
            '_m' => ($route['module']) ? $route['module'] : $defaultRoute['module'],
            '_c' => ($route['controller']) ? $route['controller'] : $defaultRoute['controller'],
            '_a' => ($route['action']) ? $route['action'] : $defaultRoute['action'],
        ), $parameters));
    }

    public function createAbsoluteUrl($route, $parameters = array()) {
        return PROTOCOL . $_SERVER['SERVER_NAME'] . '/' . $this->createUrl($route, $parameters);
    }

}
